<?php
class MtApiController  extends CommonController
{
    /*
        添加店铺信息
    */
    public function addStoreInfo()
    {
        $storeId = $_POST['store_id'];
        $storeName = $_POST['store_name'];
        $startTime = $_POST['start_time'];
        $endTime = $_POST['end_time'];
        $chn_store_id = $_POST['chn_store_id'];

        $MtApiModel = new  MtApiModel();

        $addStore = $MtApiModel->addStoreInfo($storeId, $storeName, $startTime, $endTime, $chn_store_id);

        if($addStore == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($addStore['errNo'])){
            $this->_response($addStore);
        }

        $res = array(
            "errNo" => "0",
            "data" => $addStore
        );
        
        $this->_response($res);
    }

    /*
        修改店铺信息
    */
    public function updateStoreInfo()
    {
        $chn_id = $_POST['chn_id'];
        $store_id = $_POST['store_id'];
        $other_shop_name = $_POST['other_shop_name'];
        $other_shop_desc = $_POST['other_shop_desc'];
        $start_time = $_POST['start_time'];
        $end_time = $_POST['end_time'];
        $shipping_fee = $_POST['shipping_fee'];
        $store_status = $_POST['store_status'];

        $MtApiModel = new  MtApiModel();

        $result = $MtApiModel->updateStoreInfo($chn_id, $store_id, $other_shop_name, $other_shop_desc, $start_time, $end_time, $shipping_fee, $store_status);

        if($result == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($result['errNo'])){
            $this->_response($result);
        }

        $res = array(
            "errNo" => "0",
            "data" => $result
        );
        
        $this->_response($res);
    }

    /*
        获取门店ID
    */
    public function getIds()
    {
        $MtApiModel = new  MtApiModel();

        $getIds = $MtApiModel->getIds();

        if($getIds == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($getIds['errNo'])){
            $this->_response($getIds);
        }

        $res = array(
            "errNo" => "0",
            "data" => $getIds
        );
        
        $this->_response($res);
    }

    /*
        批量获取门店详细信息
    */
    public function mget()
    {
        $app_poi_codes = $_POST['app_poi_codes'];

        $MtApiModel = new  MtApiModel();

        $data = $MtApiModel->mget($app_poi_codes);

        if($data == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($data['errNo'])){
            $this->_response($data);
        }

        $res = array(
            "errNo" => "0",
            "data" => $data
        );
        
        $this->_response($res);
    }

    /*
        门店设置为营业状态
    */
    public function openShop()
    {
        $store_id = $_POST['store_id'];

        $MtApiModel = new  MtApiModel();

        $openShop = $MtApiModel->openShop($store_id);

        if($openShop == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($openShop['errNo'])){
            $this->_response($openShop);
        }

        $res = array(
            "errNo" => "0",
            "data" => $openShop
        );
        
        $this->_response($res);
    }

    /*
        门店设置为停业状态
    */
    public function closeShop()
    {
        $store_id = $_POST['store_id'];

        $MtApiModel = new  MtApiModel();

        $closeShop = $MtApiModel->closeShop($store_id);

        if($closeShop == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($closeShop['errNo'])){
            $this->_response($closeShop);
        }

        $res = array(
            "errNo" => "0",
            "data" => $closeShop
        );
        
        $this->_response($res);
    }

    /*
        门店设置为下线状态
    */
    public function offlineShop()
    {
        $store_id = $_POST['store_id'];

        $MtApiModel = new  MtApiModel();

        $offlineShop = $MtApiModel->offlineShop($store_id);

        if($offlineShop == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($offlineShop['errNo'])){
            $this->_response($offlineShop);
        }

        $res = array(
            "errNo" => "0",
            "data" => $offlineShop
        );
        
        $this->_response($res);
    }

    /*
        更新店铺公告
    */
    public function setShopTopic()
    {
        $store_id = $_POST['store_id'];

        $topic = $_POST['topic'];

        $MtApiModel = new MtApiModel();

        $setShopTopic = $MtApiModel->setShopTopic($store_id, $topic);
        
        if($setShopTopic == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($setShopTopic['errNo'])){
            $this->_response($setShopTopic);
        }

        $res = array(
            "errNo" => "0",
            "data" => $setShopTopic
        );
        
        $this->_response($res);
    }

    /*
        更新门店营业时间
    */
    public function setBusinessTime()
    {
        $storeId = $_POST['store_id'];
        $startTime = $_POST['start_time'];
        $endTime = $_POST['end_time'];

        $MtApiModel = new MtApiModel();

        $setBusinessTime = $MtApiModel->setBusinessTime($storeId, $startTime, $endTime);
        
        if($setBusinessTime == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($setBusinessTime['errNo'])){
            $this->_response($setBusinessTime);
        }

        $res = array(
            "errNo" => "0",
            "data" => $setBusinessTime
        );
        
        $this->_response($res);
    }

    /*
        创建/更新门店配送范围
    */
    public function saveShipping()
    {
        $storeId = $_POST['store_id'];
        $app_shipping_code = 1;
        $type = 1;
        $min_price = 0;

        $MtApiModel = new MtApiModel();

        $saveShipping = $MtApiModel->saveShipping($storeId, $app_shipping_code, $type, $min_price);
        
        if($saveShipping == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($saveShipping['errNo'])){
            $this->_response($saveShipping);
        }

        $res = array(
            "errNo" => "0",
            "data" => $saveShipping
        );
        
        $this->_response($res);
    }

    /*
         获取门店配送范围
    */
    public function shippingList()
    {
        $storeId = $_POST['store_id'];

        $MtApiModel = new MtApiModel();

        $shippingList = $MtApiModel->shippingList($storeId);
        
        if($shippingList == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($shippingList['errNo'])){
            $this->_response($shippingList);
        }

        $res = array(
            "errNo" => "0",
            "data" => $shippingList
        );
        
        $this->_response($res);
    }

    /*
        初始化自定义分类
    */
    public function initCategoryInfo()
    {
        $store_id = $_POST['store_id'];

        $MtApiModel = new  MtApiModel();

        $initCategoryInfo = $MtApiModel->initCategoryInfo($store_id);

        if($initCategoryInfo == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($initCategoryInfo['errNo'])){
            $this->_response($initCategoryInfo);
        }

        $res = array(
            "errNo" => "0",
            "data" => $initCategoryInfo
        );
        
        $this->_response($res);
    }

    /*
          添加商品分类
    */
    public function saveMedicineCat()
    {
        $app_poi_code = $_POST['app_poi_code'];
        $category_code = $_POST['category_code'];
        $category_name = $_POST['category_name'];
        $sequence = $_POST['sequence'];

        $MtApiModel = new MtApiModel();

        $saveMedicineCat = $MtApiModel->saveMedicineCat($app_poi_code, $category_code, $category_name, $sequence);
        
        if($saveMedicineCat == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($saveMedicineCat['errNo'])){
            $this->_response($saveMedicineCat);
        }

        $res = array(
            "errNo" => "0",
            "data" => $saveMedicineCat
        );
        
        $this->_response($res);
    }

    /*
          修改药品分类
    */
    public function updateMedicineCat()
    {
        $app_poi_code = $_POST['app_poi_code'];
        $category_code = $_POST['category_code'];
        $category_name = $_POST['category_name'];
        $sequence = $_POST['sequence'];

        $MtApiModel = new MtApiModel();

        $updateMedicineCat = $MtApiModel->updateMedicineCat($app_poi_code, $category_code, $category_name, $sequence);
        
        if($updateMedicineCat == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($updateMedicineCat['errNo'])){
            $this->_response($updateMedicineCat);
        }

        $res = array(
            "errNo" => "0",
            "data" => $updateMedicineCat
        );
        
        $this->_response($res);
    }

    /*
        删除药品分类
    */
    public function deleteMedicineCat()
    {
        $app_poi_code = $_POST['app_poi_code'];
        $category_code = $_POST['category_code'];

        $MtApiModel = new MtApiModel();

        $deleteMedicineCat = $MtApiModel->deleteMedicineCat($app_poi_code, $category_code);
        
        if($deleteMedicineCat == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($deleteMedicineCat['errNo'])){
            $this->_response($deleteMedicineCat);
        }

        $res = array(
            "errNo" => "0",
            "data" => $deleteMedicineCat
        );
        
        $this->_response($res);
    }

    /*
        查询药品分类列表
    */
    public function medicineCatList()
    {
        $app_poi_code = $_POST['app_poi_code'];

        $MtApiModel = new MtApiModel();

        $medicineCatList = $MtApiModel->medicineCatList($app_poi_code);
        
        if($medicineCatList == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($medicineCatList['errNo'])){
            $this->_response($medicineCatList);
        }

        $res = array(
            "errNo" => "0",
            "data" => $medicineCatList
        );
        
        $this->_response($res);
    }

    /*
          添加商品
    */
    public function saveMedicine()
    {
        $store_id = $_POST['store_id'];
        $product_id = $_POST['product_id'];
        $other_category_id = $_POST['other_category_id'];

        $MtApiModel = new MtApiModel();

        $saveMedicine = $MtApiModel->saveMedicine($store_id, $product_id, $other_category_id);
        
        if($saveMedicine == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($saveMedicine['errNo'])){
            $this->_response($saveMedicine);
        }

        $res = array(
            "errNo" => "0",
            "data" => $saveMedicine
        );
        
        $this->_response($res);
    }

    /*
        修改商品
    */
    public function updateMedicine()
    {
        $store_id = $_POST['store_id'];
        $product_id = $_POST['product_id'];

        $MtApiModel = new MtApiModel();

        $updateMedicine = $MtApiModel->updateMedicine($store_id, $product_id);
        
        if($updateMedicine == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($updateMedicine['errNo'])){
            $this->_response($updateMedicine);
        }

        $res = array(
            "errNo" => "0",
            "data" => $updateMedicine
        );
        
        $this->_response($res);
    }

    /*
        批量添加药品
    */
    public function batchSaveMedicine()
    {
        $store_id = $_POST['store_id'];
        $product_ids = $_POST['product_ids'];

        $MtApiModel = new MtApiModel();

        $batchSaveMedicine = $MtApiModel->batchSaveMedicine($store_id, $product_ids);
        
        if($batchSaveMedicine == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($batchSaveMedicine['errNo'])){
            $this->_response($batchSaveMedicine);
        }

        $res = array(
            "errNo" => "0",
            "data" => $batchSaveMedicine
        );
        
        $this->_response($res);
    }

    /*
        删除药品
    */
    public function deleteMedicine()
    {
        $store_id = $_POST['store_id'];
        $app_medicine_code = $_POST['product_id'];

        $MtApiModel = new MtApiModel();

        $deleteMedicine = $MtApiModel->deleteMedicine($store_id, $app_medicine_code);
        
        if($deleteMedicine == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($deleteMedicine['errNo'])){
            $this->_response($deleteMedicine);
        }

        $res = array(
            "errNo" => "0",
            "data" => $deleteMedicine
        );
        
        $this->_response($res);
    }

    /*
        查询门店药品列表
    */
    public function medicineList()
    {
        $store_id = $_POST['store_id'];

        $MtApiModel = new MtApiModel();

        $medicineList = $MtApiModel->medicineList($store_id);
        
        if($medicineList == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($medicineList['errNo'])){
            $this->_response($medicineList);
        }

        $res = array(
            "errNo" => "0",
            "data" => $medicineList
        );
        
        $this->_response($res);
    }

    /*
        批量更新药品库存
    */
    public function medicineStock()
    {
        $data = $_POST['data'];
        $data = array(
            0 => array(
                'store_id' => 60,
                'other_product_id' => 1,
                'product_stock' => 10
            ),
            1 => array(
                'store_id' => 60,
                'other_product_id' => 2,
                'product_stock' => 10
            ),
            2 => array(
                'store_id' => 60,
                'other_product_id' => 3,
                'product_stock' => 10
            )
        );

        $MtApiModel = new MtApiModel();

        $medicineStock = $MtApiModel->syncStock($data);
        
        if($medicineStock == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($medicineStock['errNo'])){
            $this->_response($medicineStock);
        }

        $res = array(
            "errNo" => "0",
            "data" => $medicineStock
        );
        
        $this->_response($res);
    }

    /*
        批量更新药品价格
    */
    public function medicinePrice()
    {
        $data = $_POST['data'];
        $data = array(
            0 => array(
                'store_id' => 60,
                'other_product_id' => 1,
                'product_price' => 10
            ),
            1 => array(
                'store_id' => 60,
                'other_product_id' => 2,
                'product_price' => 10
            ),
            2 => array(
                'store_id' => 60,
                'other_product_id' => 3,
                'product_price' => 10
            )
        );

        $MtApiModel = new MtApiModel();

        $medicineStock = $MtApiModel->syncPrice($data);
        
        if($medicineStock == false){
            $res = array(
                "errNo" => "0003"
            );
            $this->_response($res);
        }

        if(isset($medicineStock['errNo'])){
            $this->_response($medicineStock);
        }

        $res = array(
            "errNo" => "0",
            "data" => $medicineStock
        );
        
        $this->_response($res);
    }

}
?>